package com.xiantm.study.thread.towparsetermination;

public class ThreadErrorHook {
    public static void main(String[] args) {
        System.out.println("Main Start. ");
        Thread.setDefaultUncaughtExceptionHandler((thread, exception) -> {
            System.out.println("    ****");
            System.out.println("    UncaughtExceptionHandler:BEGIN");
            System.out.println("    currentThread = " + Thread.currentThread());
            System.out.println("    thread = " + thread);
            System.out.println("    exception = " + exception);
            System.out.println("    UncaughtExceptionHandler : END");
        });

//        Thread.setDefaultUncaughtExceptionHandler(
//                new Thread.UncaughtExceptionHandler() {
//                    public void uncaughtException(Thread thread, Throwable exception) {
//                        System.out.println("****");
//                        System.out.println("UncaughtExceptionHandler:BEGIN");
//                        System.out.println("currentThread = " + Thread.currentThread());
//                        System.out.println("thread = " + thread);
//                        System.out.println("exception = " + exception);
//                        System.out.println("UncaughtExceptionHandler:END");
//                    }
//                }
//        );
        Runtime.getRuntime().addShutdownHook(new Thread(()->{
            System.out.println("    ****");
            System.out.println("    shutdown hook: BEGIN.");
            System.out.println("    currentThread = " + Thread.currentThread());
            System.out.println("    shutdown hook: END.");
        }));

        new Thread("MyThread") {
            public void run() {
                System.out.println("MyThread : BEGIN ");
                System.out.println("MyT hread: SLEEP .. .");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {

                }
                System.out.println(" MyThread:DIVIDE ");
                int x = 1 / 0;
                System.out.println(" MyThread:END ");
            }
        }.start();
        System.out.println("main:END ");
    }
}
